package com.xs.designpattern;

/**
 * 懒汉式
 * @author xs
 */
public class ThreadSafeLazyLoadedIvoryTower {
    private static volatile ThreadSafeLazyLoadedIvoryTower instance;

    private ThreadSafeLazyLoadedIvoryTower() {
        // 通过反射防止实例化
        if (instance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    /**
     * 在第一次调用该方法之前不会创建实例
     */
    public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() {
        if (instance == null) {
            instance = new ThreadSafeLazyLoadedIvoryTower();
        }
        return instance;
    }
}
